package com.tokenmaster.aitrade.trade;

import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.log4j.Logger;

import com.tokenmaster.aitrade.common.ticker.TickerData;
import com.tokenmaster.aitrade.data.SystemDB;

/* 箱体计算规则
 * 1,箱体范围应该大于9%,上下波动范围7%
 *
 * 
 */

/**
 * 箱体计算处理类
 * 
 * @Description: 计算箱体位置
 * @author buxinyun
 * @date 2018年6月12日
 *
 */
public class UpAndDown {

	private TickerData highTicker;
	private TickerData lowTicker;
	private TickerData activeTicker;
	private String direction = "UP";
	private SystemDB systemDB = SystemDB.getInstance();
	private Logger logger = Logger.getLogger(this.getClass());

	private ITradeRequest iTradeRequest;

	private double buyPrice = -1;
	private double sellPrice = -1;

	public UpAndDown(ITradeRequest iTradeRequest) {
		this.iTradeRequest = iTradeRequest;
	}

	public void newTicker(TickerData tickerData) {

		if (lowTicker == null)
			lowTicker = tickerData;

		if (highTicker == null) {
			highTicker = tickerData;
		}

		if (activeTicker == null) {
			activeTicker = tickerData;
		}

		if (direction.equals("UP")) {
			findHighTicker(tickerData);

		} else if (direction.equals("DOWN")) {
			findLowTicker(tickerData);

		}

		if (tickerData.getUnixtime() % (5 * 60 * 1000) == 0) {
			SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String timestr = sformat.format(tickerData.getUnixtime());

			logger.info(timestr + " 当前价格:" + tickerData.getLast() + "highTicker:" + highTicker.getLast() + " lowTicker:"
					+ lowTicker.getLast());
		}
	}

	/**
	 * 寻找最高价
	 * 
	 * @param ticker_Spot
	 */
	public void findHighTicker(TickerData tickerData) {

		if (tickerData.getLast() > activeTicker.getLast()) {
			activeTicker = tickerData;
			return;
		} else if (1 - tickerData.getLast() / activeTicker.getLast() > 0.03) {


			// 动态价格为最高价
			highTicker = activeTicker;

			// 保存最高价,最低价,价差
			systemDB.insertIndexUpAndDown("btc_cny", highTicker.getLast(), highTicker.getUnixtime(), direction,
					highTicker.getLast() / lowTicker.getLast() - 1, highTicker.getUnixtime() - lowTicker.getUnixtime(),
					buyPrice, sellPrice);

			// 重置为下跌趋势
			direction = "DOWN";

			// 重置动态价格
			activeTicker = tickerData;
			
			sellPrice = -1;

			return;
		} else if (1 - tickerData.getLast() / activeTicker.getLast() > 0.02) {

			if (sellPrice == -1) {
				sellPrice = tickerData.getLast();
				checkSellCondition(tickerData);
			}
		}
	}

	/**
	 * 寻找最低价
	 * 
	 * @param ticker_Spot
	 */
	public void findLowTicker(TickerData tickerData) {
		if (tickerData.getLast() < activeTicker.getLast()) {
			activeTicker = tickerData;
			return;
		} else if (tickerData.getLast() / activeTicker.getLast() - 1 > 0.03) {

			// 动态价格为最低价
			lowTicker = activeTicker;

			// 保存最低价,最高价,价差
			systemDB.insertIndexUpAndDown("btc_cny", lowTicker.getLast(), lowTicker.getUnixtime(), direction,
					1 - lowTicker.getLast() / highTicker.getLast(), lowTicker.getUnixtime() - highTicker.getUnixtime(),
					buyPrice, sellPrice);

			// 重置为上涨趋势
			direction = "UP";
			// 重置动态价格
			activeTicker = tickerData;

			buyPrice = -1;
			return;
		} else if (tickerData.getLast() / activeTicker.getLast() - 1 > 0.02) {

			if (buyPrice == -1) {
				buyPrice = tickerData.getLast();
				checkBuyCondition(tickerData);
			}
		}
	}

	private void checkBuyCondition(TickerData tickerData) {

		if (buyPrice != -1 && tickerData.getLast() <= buyPrice) {
			logger.info("达到买入条件 buyPrice:" + buyPrice + " 当前价格:" + tickerData.getLast());
			iTradeRequest.onMeetBuyCondition(tickerData);
		}

	}

	/**
	 * 检测是否达到卖出条件
	 * 
	 * @param tickerData
	 */
	private void checkSellCondition(TickerData tickerData) {
		if (sellPrice != -1 && tickerData.getLast() >= sellPrice) {
			logger.info("达到卖出条件: sellPrice:" + sellPrice + " 当前价格:" + tickerData.getLast());
			iTradeRequest.onMeetSellCondition(tickerData);
		}
	}

	/**
	 * 计算买入价
	 * 
	 * @param level
	 */
	private void calBuyPrice(double level) {
		// 根据level计算买价
		// 涨幅超过10%,则下跌1/3为买入点
		// 涨幅超过7%,则下跌1/2为买入点
		// 涨幅不足7%,则寻找下一支撑位

		if (level > 1.01) {
			// 破新高
			if (highTicker.getLast() / lowTicker.getLast() - 1 > 0.14) {
				buyPrice = (highTicker.getLast() - lowTicker.getLast()) * 0.809 + lowTicker.getLast();
			} else if (highTicker.getLast() / lowTicker.getLast() - 1 > 0.1) {
				buyPrice = (highTicker.getLast() - lowTicker.getLast()) * 0.618 + lowTicker.getLast();
			} else {
				buyPrice = lowTicker.getLast() * 1.01;

				// 查找之前的支撑位
				// List<TickerData> tickerDatas =
				// systemDB.getTickerDatas(lowTicker.getUnixtime());
				//
				// for (TickerData tickerData : tickerDatas) {
				//
				// // 离最高点大于7%
				// if (highTicker.getLast() / tickerData.getLast() - 1 > 0.07) {
				// buyPrice = tickerData.getLast();
				// return;
				// }
				// }
			}
		} else if (level <= 1.01) {
			// 未破新高
			if (highTicker.getLast() / lowTicker.getLast() - 1 > 0.1) {
				buyPrice = highTicker.getLast() - (highTicker.getLast() - lowTicker.getLast()) * 0.618;
			} else {
				// 查找之前的支撑位
				List<TickerData> tickerDatas = systemDB.getTickerDatas(lowTicker.getUnixtime());

				for (TickerData tickerData : tickerDatas) {

					// 离最高点大于7%
					if (highTicker.getLast() / tickerData.getLast() - 1 > 0.07) {
						buyPrice = tickerData.getLast();
						return;
					}
				}
				// 下跌7%为买入价
				buyPrice = highTicker.getLast() - highTicker.getLast() * 0.07;
				return;
			}
		}

	}

	/**
	 * 计算卖出价
	 * 
	 * @param level
	 */
	private void calSellPrice(double level) {
		// 下跌超过10%,则上涨1/3为卖出点
		// 下跌超过超过7%,则上涨1/2为卖出点
		// 下跌不足7%,则寻找上次高点为卖出价

		if (level < 1) {
			// 破新低
			if (1 - lowTicker.getLast() / highTicker.getLast() > 0.1) {
				sellPrice = highTicker.getLast() - (highTicker.getLast() - lowTicker.getLast()) * 0.618;
			} else if (highTicker.getLast() / lowTicker.getLast() - 1 > 0.07) {
				sellPrice = highTicker.getLast() - (highTicker.getLast() - lowTicker.getLast()) * 0.5;
			} else {
				// 查找之前的最高价
				List<TickerData> tickerDatas = systemDB.getTickerDatas(lowTicker.getUnixtime());

				for (TickerData tickerData : tickerDatas) {

					// 离最低点大于7%的压力位
					if (1 - lowTicker.getLast() / tickerData.getLast() > 0.07) {
						sellPrice = tickerData.getLast();
						return;
					}
				}
				// 不到上次最高价位置为卖出价
				sellPrice = highTicker.getLast() * 0.99;
				return;
			}
		} else {
			// 未破新低
			if (1 - lowTicker.getLast() / highTicker.getLast() > 0.1) {
				sellPrice = highTicker.getLast() - (highTicker.getLast() - lowTicker.getLast()) * 0.618;
			} else if (highTicker.getLast() / lowTicker.getLast() - 1 > 0.07) {
				sellPrice = highTicker.getLast() - (highTicker.getLast() - lowTicker.getLast()) * 0.5;
			} else {
				// 查找之前的最高价
				List<TickerData> tickerDatas = systemDB.getTickerDatas(lowTicker.getUnixtime());

				for (TickerData tickerData : tickerDatas) {

					// 离最低点大于7%
					if (1 - lowTicker.getLast() / tickerData.getLast() > 0.07) {
						sellPrice = tickerData.getLast();
						return;
					}
				}
				// 上涨7%为卖出价
				sellPrice = lowTicker.getLast() + lowTicker.getLast() * 0.07;
				return;
			}

		}
	}

}
